package cn.gson.crm.controller;



import cn.gson.crm.model.mapper.SellMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.SellService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/de")
public class ControllerDome {
    @Autowired
    SellService sellService;
    @Autowired
    SellMapper sellMapper;
    //条件查询
    @RequestMapping("/Sell")
    @ResponseBody
    public Map<String,Object> salesAtList(Integer currentPage, Integer Pagesize,String na){
        Page<Object> obj= PageHelper.startPage(currentPage,Pagesize);
        sellService.select1(na);
        Map<String,Object> map=new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("row",obj.getResult());
        return map;
    }
    //删除
    @RequestMapping("/Sell2")
    @ResponseBody
    public void update1(int sellId){
        sellService.update1("已作废",sellId);
    }
    //员工查询
    @RequestMapping("/selectEmployee")
    @ResponseBody
    public List<Employee> selectEmployee(){
            return sellService.selectEmployee();
        }
    //新增
    @RequestMapping("/addSell")
    @ResponseBody
    public void addSell(@RequestBody Sell sell,HttpSession session){
        sellService.addSell(sell,session);
    }
    //查询饼状统计图
    @RequestMapping("/sellTu")
    @ResponseBody
    public Map<String,Object> sellType(String vv){
        Map<String,Object> map = null;
        if(vv.equals("费用类别分布")) {
            map = sellService.selltu();
        }
        else{
            map = sellService.selltu2();
        }
        return map;

    }
    //查询柱状图
    @RequestMapping("/sellzhu")
    @ResponseBody
    public Map<String,Object> sellzhu(String zz){
        if(zz.equals("员工销售分布")){
            return  sellService.sellzhu();
        }
        else {
            return  sellService.sellzhu2();
        }
    }
    //获取当前人
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=sellMapper.selectUserRole(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return 0;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }


}
